<?php

declare(strict_types=1);

namespace App\Controller\Rbac;

use App\Controller\AbstractController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Context;
use App\Validate\PermissionValidation;
use App\Service\Rbac\PermissionService;

class PermissionController extends AbstractController
{
    /**
     * @Inject
     * @var PermissionService
     */
    protected $permissionService;

    // 获取当前登录用户的权限列表
    public function menu()
    {
        $jwt = Context::get('jwt_token');
        $user_id = $jwt['uid'];
        $result = $this->permissionService->menu($user_id);
        return $this->success('ok',$result);
    }

    // 获取所有的权限列表
    public function permissionList()
    {
        $result = $this->permissionService->menu(config('web.super')[0]);
        return $this->success('ok',$result);
    }

    public function permissionAdd()
    {
        $data = $this->request->inputs(['sort_order','parent_id','url','identity','name']);
        make(PermissionValidation::class)->check($data, __FUNCTION__)->response();
        $result = $this->permissionService->addPermssion($data);
        return $this->success('ok',$result);
    }

    public function permissionEdit()
    {
        if ($this->request->isMethod('POST')){
            $data = $this->request->inputs(['id','identity','name','sort_order','parent_id','url']);
            make(PermissionValidation::class)->check($data, __FUNCTION__)->response();
            $this->permissionService->editPermissionPost($data);
            return $this->success('修改成功',true);
        }
        make(PermissionValidation::class)->check($this->request->all(), 'permissionGet')->response();
        $result = $this->permissionService->editPermissinGet((int)$this->request->input('id'));
        return $this->success('ok',$result);
    }

    public function permissionDel()
    {
        make(PermissionValidation::class)->check($this->request->all(), 'permissionGet')->response();
        $this->permissionService->delPermission((int)$this->request->input('id'));
        return $this->success('删除成功');
    }

}
